package de.koller.reason.math;

import Jama.Matrix;
import de.koller.oceanFramework.math.RVec3;

public class FragmentInterpolation {

	private final RVec3[] fragmentPos;
	private final float[] value;

	private RVec3 p = null;
	private RVec3 n = null;
	
	public FragmentInterpolation( RVec3[] fragmentPos, float[] value ) {
		this.fragmentPos = fragmentPos;
		this.value = value;
	}
	
	public float getValueAt( float x, float y ) {
		if( n == null )
			calcNormal();
		
		return ( n.x * (-x + p.x) + n.y * (-y + p.y) + n.z*p.z ) / n.z;
	}
		
	private void calcNormal() {
		p = new RVec3( fragmentPos[0].x, fragmentPos[0].y, value[0] );
		RVec3 v1 = new RVec3( fragmentPos[1].x, fragmentPos[1].y, value[1] ).sub( p );
		RVec3 v2 = new RVec3( fragmentPos[2].x, fragmentPos[2].y, value[2] ).sub( p );
		n = RVec3.cross( v1, v2 );
	}
	
}
